﻿using GalaSoft.MvvmLight;
using HRM.Model;
using GalaSoft.MvvmLight.Ioc;
using System.Collections.Generic;
using GalaSoft.MvvmLight.Messaging;
using HRM.Helper;
using GalaSoft.MvvmLight.Command;

namespace HRM.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class UserViewModel : ViewModelGeneric<NguoiSuDung>
    {
        private IUserService _userService;
        private IEntityService<NhomNguoiDung> _groupService;
        private List<NhanVien> _liststaffs;

        /// <summary>
        /// Initializes a new instance of the UserViewModel class.
        /// </summary>
        public UserViewModel(IEntityService<NguoiSuDung> service, IUserService userService, IEntityService<NhomNguoiDung> groupService)
            : base(service)
        {
            _userService = userService;
            _groupService = groupService;
            ReLoad();
        }

        private List<NhanVien> _staffs;

        public List<NhanVien> Staffs
        {
            get { return _staffs; }
            set
            {
                if (_staffs != value)
                {
                    _staffs = value;
                    RaisePropertyChanged("Staffs");
                }
            }
        }

        private NhanVien _staffSelected;

        public NhanVien StaffSelected
        {
            get
            {
                return _staffSelected;
            }
            set
            {
                if (_staffSelected != value)
                {
                    _staffSelected = value;
                    if (value != null)
                    {
                        SelectedItem.MaNhanVien = _staffSelected.MaNhanVien;
                    }
                    RaisePropertyChanged("StaffSelected");
                }
            }
        }

        private List<NhomNguoiDung> _groups;

        public List<NhomNguoiDung> Groups
        {
            get { return _groups; }
            set
            {
                if (_groups != value)
                {
                    _groups = value;
                    RaisePropertyChanged("Groups");
                }
            }
        }

        private NhomNguoiDung _groupSelected;

        public NhomNguoiDung GroupSelected
        {
            get { return _groupSelected; }
            set
            {
                if (_groupSelected != value)
                {
                    _groupSelected = value;
                    RaisePropertyChanged("GroupSelected");
                }
            }
        }

        public override bool UpdateMode
        {
            get
            {
                return base.UpdateMode;
            }
            set
            {
                if (_updateMode != value)
                {
                    _updateMode = value;
                    if (value)
                    {
                        if (SelectedItem != null)
                        {
                            if (SelectedItem.NhanVien != null && SelectedItem.NhomNguoiDung != null)
                            {
                                if (Staffs.Count > 0)
                                {
                                    Staffs[0] = SelectedItem.NhanVien;
                                }
                                else
                                {
                                    Staffs.Add(SelectedItem.NhanVien);
                                }
                                StaffSelected = Staffs[0];
                                SelectedItem.MaNhanVien = StaffSelected.MaNhanVien;
                                GroupSelected = Groups.Find(x => x.MaNhom == SelectedItem.NhomNguoiDung.MaNhom);
                            }
                        }
                    }
                    else
                    {
                        Staffs = _liststaffs;
                        StaffSelected = null;
                        GroupSelected = null;
                    }
                    RaisePropertyChanged("UpdateMode");
                }
            }
        }

        public override NguoiSuDung SelectedItem
        {
            get
            {
                return base.SelectedItem;
            }
            set
            {
                if (_selectedItem != value)
                {
                    _selectedItem = value;
                    if (value != null)
                    {
                        if (!UpdateMode) UpdateMode = true;
                        Staffs[0] = SelectedItem.NhanVien;
                        StaffSelected = Staffs[0];
                        GroupSelected = Groups.Find(x => x.MaNhom == SelectedItem.NhomNguoiDung.MaNhom);
                    }
                    RaisePropertyChanged("SelectedItem");
                }

            }
        }

        public override RelayCommand SaveCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    if (SelectedItem == null) return;

                    if (SelectedItem.TenDangNhap == null || GroupSelected == null || StaffSelected == null)
                    {
                        Messenger.Default.Send(new StatusMessage("Không được bỏ trống các trường", 3000, StatusType.Warning));
                    }
                    else
                    {
                        SelectedItem.MaNhom = GroupSelected.MaNhom;
                        SelectedItem.MaNhanVien = StaffSelected.MaNhanVien;
                        if (AddMode)
                        {
                            if (_service.Add(SelectedItem)) Messenger.Default.Send(StatusMessage.CompletedMessage());
                        }
                        else if (UpdateMode)
                        {
                            if (_service.Update(SelectedItem)) Messenger.Default.Send(StatusMessage.CompletedMessage());
                        }
                        Refresh();
                        ReLoad();
                    }
                }, () => _user.GetPermisstion);
            }
        }

        public void ReLoad()
        {
            Staffs = _userService.GetStaffsNoAccount();
            _liststaffs = Staffs;
            Groups = _groupService.GetAll();

            if (Staffs.Count > 0) StaffSelected = Staffs[0];
            if (Groups.Count > 0) GroupSelected = Groups[0];
        }
    }
}